package com.book;

import com.book.controller.BookController;
import com.book.model.Result.Result;
import com.book.pojo.Book;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * author:rengl
 * 2023-04-08 7:25
 * 控制台交互类
 * 显示菜单模拟客户端
 * 采集输入
 * 封装对象
 * 调用控制层
 */
public class Main {

   public static void init(){
       System.out.println("====================图书管理系统==============");
       System.out.println("0-退出");
       System.out.println("1-增加");
       System.out.println("2-删除");
       System.out.println("3-修改");
       System.out.println("4-查询所有");
       System.out.println("5-查询一个");
       System.out.println("请根据上述提示输入选择操作:");
   }

    private static Book add(Scanner scanner) {
        System.out.println("请输入书名:");
        String name = scanner.next();
        System.out.println("请输入作者:");
        String author = scanner.next();
        System.out.println("请输入页数:");
        Integer page = scanner.nextInt();

        //封装
        return new Book(name,author,page);
    }

    private static void showList(List<Book> bookList) {
        if(bookList != null && bookList.size()>0){
            for (Book book : bookList) {
                System.out.print("id:"+book.getId() + " ");
                System.out.print("名称:"+book.getName() + " ");
                System.out.print("作者:"+book.getAuthor()+ " ");
                System.out.print("页数:"+ book.getPage()+ " ");
                System.out.println();
            }
        }

    }

    private static Integer findOne(Scanner scanner) {
        System.out.println("请输入id:");
        int id = scanner.nextInt();
        return id;
    }

    private static Book update(Scanner scanner) {
        System.out.println("请输入要修改的id:");
        int id = scanner.nextInt();
        System.out.println("请输入修改后书名:");
        String name = scanner.next();
        System.out.println("请输入修改后作者:");
        String author = scanner.next();
        System.out.println("请输入修改后页数:");
        Integer page = scanner.nextInt();
        return new Book(id,name,author,page);
    }

    private static Integer delete(Scanner scanner) {
        System.out.println("请输入要删除的id:");
        int id = scanner.nextInt();
        return id;

    }


    public static void main(String[] args) {

        BookController bookController = new BookController();

       while(true){
           //页面初始化
           Main.init();

           //阻塞，等待输入
           Scanner scanner = new Scanner(System.in);
           int flag = scanner.nextInt();
           if(flag == 0) break;

           switch (flag){
               case 1:
                   Book book = Main.add(scanner);
                   bookController.add(book);
                   Result addresult = bookController.findAll();
                   List<Book> addbookList = (List<Book>) addresult.getData();
                   Main.showList(addbookList);
                   break;
               case 2:
                   Integer deleteid = Main.delete(scanner);
                   bookController.delete(deleteid);
                   bookController.delete(deleteid);
                   Result deleteresult = bookController.findAll();
                   List<Book> deletebookList = (List<Book>) deleteresult.getData();
                   Main.showList(deletebookList);
                   break;
               case 3:
                   Book updatebook = Main.update(scanner);
                   Result updateresult = bookController.update(updatebook);
                   Result updateResult = bookController.findOne(updatebook.getId());
                   Book updateBook = (Book) updateResult.getData();
                   List<Book> updatelist = new ArrayList();
                   updatelist.add(updateBook);
                   Main.showList(updatelist);
                   break;
               case 4:
                   Result result = bookController.findAll();
                   List<Book> bookList = (List<Book>) result.getData();
                   Main.showList(bookList);
                   break;
               case 5:
                   Integer id = Main.findOne(scanner);
                   Result OneResult = bookController.findOne(id);
                   Book OneBook = (Book) OneResult.getData();
                   List<Book> list = new ArrayList();
                   list.add(OneBook);
                   Main.showList(list);
                   break;
           }


       }


    }




}
